package sz.szsmk.citizencard.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import sz.szsmk.citizencard.R;
 

public class BarLineLayout extends LinearLayout {
    private TextView tv_label;
    private ImageView iv_left;
    public BarLineLayout(Context context) {
        super(context);
        inflateContent(context,null);
    }

    public BarLineLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflateContent(context,attrs);
    }

    public BarLineLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflateContent(context,attrs);
    }

    private void inflateContent(Context context, AttributeSet attrs) {
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.qr_BarLineLayout);
        String labelText = a.getString(R.styleable.qr_BarLineLayout_qr_label_text);
        boolean showBottomLine = a.getBoolean(R.styleable.qr_BarLineLayout_qr_show_bottom_line, false);
        int iconId = a.getResourceId(R.styleable.qr_BarLineLayout_qr_icon_id, -1);
        a.recycle();

        setOrientation(LinearLayout.VERTICAL);
        RelativeLayout trl = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.qr_item_item_bar, null, false);
        int heightPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
        LayoutParams trllp = new LayoutParams(LayoutParams.MATCH_PARENT,heightPx);
        addView(trl,trllp);

        if (showBottomLine){
            View view = new View(getContext());
            view.setBackgroundResource(R.color.qr_line_main);
            int sixteen = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics());
            LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,3);
            lp.leftMargin = sixteen;
            addView(view,lp);
        }

        tv_label = (TextView) trl.findViewById(R.id.tv_label);
        if (!TextUtils.isEmpty(labelText)){
            tv_label.setText(labelText);
        }

        iv_left = (ImageView) trl.findViewById(R.id.iv_left);
        if (-1 != iconId){
            iv_left.setImageResource(iconId);
            iv_left.setVisibility(View.VISIBLE);
        }else{
            iv_left.setVisibility(View.GONE);
        }

    }

    public void setTextAndImage(String text,int imageCode){
        if (-1 != imageCode){
            iv_left.setImageResource(imageCode);
        }
        if (!TextUtils.isEmpty(text)){
            tv_label.setText(text);
        }
    }


}